package com.hyj.boxueguxiangmu.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hyj.boxueguxiangmu.R;
import com.hyj.boxueguxiangmu.model.LoginModel;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


public class LongActivity extends AppCompatActivity {

     EditText mUsername,mPassword;

    public void back(View view){
        Intent intent = new Intent(LongActivity.this,MainActivity.class);
        startActivity(intent);

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_long);
        mUsername = (EditText) findViewById(R.id.username);
        mPassword = (EditText) findViewById(R.id.password);
    }
    public void login(View view){
        RequestParams params = new RequestParams("http://1633.applinzi.com/public/?s=App.User.Login");
        params.addQueryStringParameter("username", mUsername.getText().toString());
        params.addQueryStringParameter("password", mPassword.getText().toString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                LoginModel  loginModel = gson.fromJson(result,LoginModel.class);
                if (loginModel.code==200){
                    SharedPreferences sp  = getSharedPreferences("login", Context.MODE_APPEND);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putBoolean("islogin",true);
                    editor.commit();
                    finish();
                }else {
                    Toast.makeText(LongActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
                }
                Log.i("LongActivity","onSuccess result:" +result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("LongActivity","onError result:" +ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {

            }
        });
    }
    public void registered(View view){
        Intent intent = new Intent(LongActivity.this,RegisteredActivity.class);
        startActivity(intent);
    }
}
